// 轮播图功能实现
document.addEventListener('DOMContentLoaded', function() {
    const slidesContainer = document.querySelector('.carousel-slides');
    const slides = document.querySelectorAll('.carousel-slide');
    const indicators = document.querySelectorAll('.indicator');
    const prevBtn = document.querySelector('.prev');
    const nextBtn = document.querySelector('.next');
    let currentIndex = 0;
    const slideCount = slides.length;
    let slideInterval;

    // 初始化轮播
    function initCarousel() {
        updateSlidePosition();
        startAutoSlide();
    }

    // 更新幻灯片位置
    function updateSlidePosition() {
        slidesContainer.style.transform = `translateX(-${currentIndex * 100}%)`;
        
        // 更新指示器状态
        indicators.forEach((indicator, index) => {
            indicator.classList.toggle('active', index === currentIndex);
        });
    }

    // 下一张幻灯片
    function nextSlide() {
        currentIndex = (currentIndex + 1) % slideCount;
        updateSlidePosition();
    }

    // 上一张幻灯片
    function prevSlide() {
        currentIndex = (currentIndex - 1 + slideCount) % slideCount;
        updateSlidePosition();
    }

    // 跳转到指定幻灯片
    function goToSlide(index) {
        currentIndex = index;
        updateSlidePosition();
    }

    // 自动轮播
    function startAutoSlide() {
        slideInterval = setInterval(nextSlide, 2000); // 每2秒切换一次
    }

    // 暂停自动轮播
    function pauseAutoSlide() {
        clearInterval(slideInterval);
    }

    // 事件监听
    nextBtn.addEventListener('click', () => {
        pauseAutoSlide();
        nextSlide();
        startAutoSlide();
    });

    prevBtn.addEventListener('click', () => {
        pauseAutoSlide();
        prevSlide();
        startAutoSlide();
    });

    indicators.forEach(indicator => {
        indicator.addEventListener('click', () => {
            pauseAutoSlide();
            goToSlide(parseInt(indicator.dataset.index));
            startAutoSlide();
        });
    });

    // 鼠标悬停时暂停轮播
    slidesContainer.addEventListener('mouseenter', pauseAutoSlide);
    slidesContainer.addEventListener('mouseleave', startAutoSlide);

    // 初始化
    initCarousel();
});